package com.wss.common.utils.toast;

import ohos.aafwk.ability.Ability;
import ohos.aafwk.ability.AbilityPackage;
import ohos.agp.window.dialog.ToastDialog;
import ohos.agp.window.service.WindowManager;
import ohos.eventhandler.EventHandler;
import ohos.eventhandler.EventRunner;

public class ToastHelper extends EventHandler {

    /**
     * 当前的吐司对象
     */
    private final ToastDialog mToast;

    /**
     * WindowManager 辅助类
     */
    private final WindowHelper mWindowHelper;

    /**
     * 当前应用的包名
     */
    private final String mPackageName;

    /**
     * 当前是否已经显示
     */
    private boolean mShow;

    ToastHelper(ToastDialog toast, AbilityPackage application) {
        super(EventRunner.getMainEventRunner());
        mToast = toast;
        mPackageName = application.getBundleName();
        mWindowHelper = WindowHelper.register(this, application);
    }

    boolean isShow() {
        return mShow;
    }

    void setShow(boolean show) {
        mShow = show;
    }

    /**
     * 显示吐司弹窗
     */
    void show() {
        if (!isShow()) {
            final WindowManager.LayoutConfig params = new WindowManager.LayoutConfig();

            params.flags = WindowManager.LayoutConfig.MARK_LOCK_AS_SCREEN_ON
                    | WindowManager.LayoutConfig.MARK_FOCUSABLE_IMPOSSIBLE
                    | WindowManager.LayoutConfig.MARK_TOUCHABLE_IMPOSSIBLE;

            // 重新初始化位置
            params.alignment = mToast.getWindow().getLayoutConfig().get().alignment;

            Ability topAbility = mWindowHelper.getTopAbility();
            if (topAbility != null && !topAbility.isTerminating()) {
                //TODO 添加view到window
            }
        }
    }

    /**
     * 取消吐司弹窗
     */
    void cancel() {
        //TODO 移除之前的吐司任务
    }
}
